package li.buda.cqflex.commandbus {
import asmock.framework.Expect;
import asmock.framework.MockRepository;

import asmock.framework.SetupResult;
import asmock.integration.flexunit.ASMockClassRunner;

import flash.net.getClassByAlias;

import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

import org.flexunit.Assert;

[RunWith("asmock.integration.flexunit.ASMockClassRunner")]
public class SimpleCommandBusTest {
    private var runner:ASMockClassRunner;
    [Mock]
    public static var mockCommandHandlerClass:CommandHandler;
    [Mock]
    public static var mockCommandClass:Command;
    private var testSubject:SimpleCommandBus;

    public function SimpleCommandBusTest() {
    }

    [Before]
    public function setup():void {
        testSubject = new SimpleCommandBus();
    }

    [After]
    public function tearDown():void {
        testSubject = null;
    }

    [Test(description="Tests the behaviour of SimpleCommandBus#publish when no command handler is subscribed for the given command.",
            expects="li.buda.cqflex.commandbus.NoCommandHandlerError")]
    public function testPublishCommandNoCommandHandler():void {
        var mockRepository:MockRepository = new MockRepository();
        var mockCommand:Command = Command(mockRepository.createStub(Command));
        mockRepository.replay(mockCommand);
        testSubject.publish(mockCommand);
    }

    [Test(description="Verify an error is thrown when a null command is passed to SimpleCommandBus#publish",
            expects="li.buda.cqflex.validate.ValidateError")]
    public function testPublishNullCommand():void {
        testSubject.publish(null);
    }

    [Test(description="Tests the behaviour of SimpleCommandBus#subscribe when null command type is given.",
            expects="li.buda.cqflex.validate.ValidateError")]
    public function testSubscribeNoCommandValidateError():void {
        testSubject.subscribe(null, null);
    }

    [Test(description="Tests the behaviour of SimpleCommandBus#subscribe when null command handler is given.",
            expects="li.buda.cqflex.validate.ValidateError")]
    public function testSubscribeNoCommandHandlerValidateError():void {
        testSubject.subscribe(Class, null);
    }

    [Test(description="Tests the behaviour of SimpleCommandBus#unsubscribe when null command type is given.",
            expects="li.buda.cqflex.validate.ValidateError")]
    public function testUnsubscribeNoCommandValidateError():void {
        testSubject.unsubscribe(null);
    }

    [Test(description="Tests the behaviour of the SimpleCommandBus#unsubscribe method when unrelated command handler is given.",
            expects="li.buda.cqflex.commandbus.NoCommandHandlerError")]
    public function testUnsubscribeNonExistantHandler():void {
        var mockRepository:MockRepository = new MockRepository();
        var mockHandler:CommandHandler = CommandHandler(mockRepository.createStub(CommandHandler));
        mockRepository.replay(mockHandler);
        testSubject.unsubscribe(Class, mockHandler);
    }
}
}